package com.aruba.bulethoothdemoapplication.adapter;

import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.recyclerview.widget.RecyclerView;

import com.aruba.bulethoothdemoapplication.databinding.FragmentItemBinding;
import com.aruba.bulethoothdemoapplication.model.BleDevice;
import com.aruba.bulethoothdemoapplication.view.SelectDeviceFragment.OnListFragmentInteractionListener;

import java.util.List;

public class MyItemRecyclerViewAdapter extends RecyclerView.Adapter<BindingViewHolder> {

    private List<BleDevice> mValues;
    private final OnListFragmentInteractionListener mListener;

    public MyItemRecyclerViewAdapter(OnListFragmentInteractionListener listener) {
        mListener = listener;
    }

    @Override
    public BindingViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        FragmentItemBinding binding = FragmentItemBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false);
        return new BindingViewHolder(binding);
    }

    @Override
    public void onBindViewHolder(final BindingViewHolder holder, int position) {
        ((FragmentItemBinding) holder.getBinding()).setDevice(mValues.get(position));
        holder.itemView.setOnClickListener(v -> {
            if (null != mListener) {
                mListener.onListFragmentInteraction(((FragmentItemBinding) holder.getBinding()).getDevice());
            }
        });
        holder.getBinding().executePendingBindings();
    }

    @Override
    public int getItemCount() {
        return mValues == null ? 0 : mValues.size();
    }

    public void setValues(List<BleDevice> mValues) {
        this.mValues = mValues;
    }
}
